/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_TEXTUREATLAS_IMPL__
#define __CC_PYTHON_IMPORT_O_TEXTUREATLAS_IMPL__

#include "python_object_textureatlas.h"
#include "python_object_texture2d.h"

NS_CCPY_BEGIN
	CC_OBJECT_WRAPPER_IMPL(CCTextureAtlas, CPyTextureAtlas)
	
	GLushort*           __CCTextureAtlasWrapper::get_m_pIndices(){ return this->m_pIndices; }
	void				__CCTextureAtlasWrapper::set_m_pIndices(GLushort* v){ this->m_pIndices = v; }
#if CC_TEXTURE_ATLAS_USE_VAO
	GLuint				__CCTextureAtlasWrapper::get_m_uVAOname(){ return this->m_uVAOname; }
	void				__CCTextureAtlasWrapper::set_m_uVAOname(GLuint v){ this->m_uVAOname = v; }
#endif
	GLuint              __CCTextureAtlasWrapper::get_m_pBuffersVBO(int index){ return m_pBuffersVBO[index]; }
	void				__CCTextureAtlasWrapper::set_m_pBuffersVBO(int index, GLuint v){ m_pBuffersVBO[index] = v; }
	bool                __CCTextureAtlasWrapper::get_m_bDirty(){ return m_bDirty; }
	void				__CCTextureAtlasWrapper::set_m_bDirty(bool b){ m_bDirty = b; }
	

	unsigned int		__CCTextureAtlasWrapper::_TotalQuads(){ return this->getTotalQuads(); }
	unsigned int		__CCTextureAtlasWrapper::_Capacity(){ return this->getCapacity(); }
	cocos2d::CCTexture2D *		__CCTextureAtlasWrapper::_Texture(){ return this->getTexture(); }
	cocos2d::ccV3F_C4B_T2F_Quad * __CCTextureAtlasWrapper::_Quads(){ return this->getQuads(); };
	
	PYCLASS_INIT_AND_BASE(CPyTextureAtlas, CPyCCObject, "CCTextureAtlas", "Python CCTextureAtlas Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyTextureAtlas)
	PYCLASS_ADD_METHOD_EX(CPyTextureAtlas, moveQuadsFromIndex	, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_ADD_METHOD_EX(CPyTextureAtlas, drawNumberOfQuads	, METH_KEYWORDS|METH_VARARGS)
	
    PYCLASS_ADD_METHOD(CPyTextureAtlas, initWithFile)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, initWithTexture)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, updateQuad)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, insertQuad)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, insertQuads)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, insertQuadFromIndex)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, removeQuadAtIndex)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, removeQuadsAtIndex)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, removeAllQuads)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, resizeCapacity)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, increaseTotalQuadsWith)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, fillWithEmptyQuadsFromIndex)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, drawQuads)
    PYCLASS_ADD_METHOD(CPyTextureAtlas, listenBackToForeground)

	PYCLASS_END_METHOD_DEF(CPyTextureAtlas)
	PYCLASS_BEGIN_GETSET_DEF(CPyTextureAtlas)

    PYCLASS_ADD_GETSET(CPyTextureAtlas, _pIndices)
#if CC_TEXTURE_ATLAS_USE_VAO
    PYCLASS_ADD_GETSET(CPyTextureAtlas, _uVAOname)
#endif
    PYCLASS_ADD_GETSET(CPyTextureAtlas, _pBuffersVBO_1)
    PYCLASS_ADD_GETSET(CPyTextureAtlas, _pBuffersVBO_2)
    PYCLASS_ADD_GETSET(CPyTextureAtlas, _bDirty)
    PYCLASS_ADD_GET(CPyTextureAtlas, _TotalQuads)
    PYCLASS_ADD_GET(CPyTextureAtlas, _Capacity)
    PYCLASS_ADD_GET(CPyTextureAtlas, _Texture)
    PYCLASS_ADD_GET(CPyTextureAtlas, _Quads)

	PYCLASS_END_GETSET_DEF(CPyTextureAtlas)
	PYCLASS_IMP_INTERFACE_NEW(CPyTextureAtlas)
		
	int CPyTextureAtlas::__init__(CPyTextureAtlas *pySelf, PyObject *args, PyObject *kwds)
	{
		pySelf->setRaw();
		static char *kwlist[] = {"capacity", "file", "texture", NULL};
		char *file = "";
		PyObject *tex = NULL;
		unsigned int cap = 0;
		if(!PyArg_ParseTupleAndKeywords(args,kwds,"I|sO",kwlist,&cap,&file,&tex))
			return 0;
		if( (file && strlen(file) >= 0) )
		{
			CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas,  initWithFile, file, cap);
		}else if( (PyObject_TypeCheck(tex, CPyClassBase::GetTypeObject<CPyTexture2D>())) )
		{
			CPyTexture2D *tex2d = (CPyTexture2D *)tex;
			CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas,  initWithTexture, (cocos2d::CCTexture2D *)tex2d->getCCObject(), cap);
		}

		return 0;
	}

	void CPyTextureAtlas::__del__(CPyTextureAtlas *self)
	{
		self->ob_type->tp_free(self);
	}
	
	PYCLASS_METHOD_KW_IMPL(CPyTextureAtlas, moveQuadsFromIndex)
	{
		if( !pyArgs )
			Py_RETURN_NONE;
		static char* kwlist[] = {"index", "newindex", "amount", NULL};
		unsigned int index = 0;
		unsigned int newindex = 0;
		int amount = -1;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"II|i",kwlist, &index, &newindex, &amount))
			Py_RETURN_NONE;
		
		if(amount >= 0)
		{
			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas, moveQuadsFromIndex, index, amount, newindex);
		}
		else			
			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas, moveQuadsFromIndex, index, newindex);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_KW_IMPL(CPyTextureAtlas, drawNumberOfQuads)
	{
		if( !pyArgs )
			Py_RETURN_NONE;
		static char* kwlist[] = {"n", "start", NULL};
		unsigned int n = 0;
		int start = -1;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"I|i",kwlist, &n, &start))
			Py_RETURN_NONE;
		
		if(start >= 0)
		{
			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas, drawNumberOfQuads, n, start);
		}
		else			
			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas, drawNumberOfQuads, n);
		Py_RETURN_NONE;
	}
	
    PYCLASS_METHOD_IMPL(CPyTextureAtlas, initWithFile)
	{
		char *file = "";
		int cap = 0;
		if( !PyArg_ParseTuple(pyArgs, "si", &file, &cap) )
			Py_RETURN_FALSE;
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas, initWithFile, bool, file, cap);
		Py_RETURN_BOOL(v);
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, initWithTexture)
	{
		PyObject *tex = NULL;
		int cap = 0;
		if( !PyArg_ParseTuple(pyArgs, "Oi", &tex, &cap) )
			Py_RETURN_FALSE;

		if( (PyObject_TypeCheck(tex, CPyClassBase::GetTypeObject<CPyTexture2D>())) )
		{
			CPyTexture2D *tex2d = (CPyTexture2D *)tex;
			CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  initWithTexture, bool, (cocos2d::CCTexture2D *)tex2d->getCCObject(), cap);
		
			Py_RETURN_BOOL(v);
		}
		Py_RETURN_FALSE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, updateQuad)
	{
		PyObject *vt = NULL;
		unsigned int index = 0;
		if( !PyArg_ParseTuple(pyArgs, "OI", &vt, &index) )
			Py_RETURN_NONE;

		if( (PyObject_TypeCheck(vt, CPyClassBase::GetTypeObject<CPyV3F_C4B_T2F_Quad>())) )
		{
			CPyV3F_C4B_T2F_Quad *vt2d = (CPyV3F_C4B_T2F_Quad *)vt;
			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  updateQuad, &vt2d->getCCObject(), index);
		}
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, insertQuad)
	{
		PyObject *vt = NULL;
		unsigned int index = 0;
		if( !PyArg_ParseTuple(pyArgs, "OI", &vt, &index) )
			Py_RETURN_NONE;

		if( (PyObject_TypeCheck(vt, CPyClassBase::GetTypeObject<CPyV3F_C4B_T2F_Quad>())) )
		{
			CPyV3F_C4B_T2F_Quad *vt2d = (CPyV3F_C4B_T2F_Quad *)vt;
			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  insertQuad, &vt2d->getCCObject(), index);
		}
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, insertQuads)
	{
		PyObject *vts = NULL;
		unsigned int index = 0;
		if( !PyArg_ParseTuple(pyArgs, "OI", &vts, &index) )
			Py_RETURN_NONE;

		cocos2d::ccV3F_C4B_T2F_Quad *vtses = NULL;
		if( PySequence_Check(vts) )
		{
			Py_ssize_t amount = PySequence_Length(vts);
			if(!amount)
				Py_RETURN_NONE;
			vtses = new cocos2d::ccV3F_C4B_T2F_Quad [amount];
			for(int i = 0; i < amount; i ++)
			{
				PyObject *o = PySequence_GetItem(vts, i);
				if(!o)
					goto __ext_fn;
				if( !(PyObject_TypeCheck(o, CPyClassBase::GetTypeObject<CPyV3F_C4B_T2F_Quad>())) )
					goto __ext_fn;
				CPyV3F_C4B_T2F_Quad *vtp = (CPyV3F_C4B_T2F_Quad *)o;
				vtses[i] = vtp->getCCObject();
				//Py_DECREF(o);
			}

			CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  insertQuads, vtses, index, amount);
		}
__ext_fn:
		if(vtses)
			delete[] vtses;
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, insertQuadFromIndex)
	{
		unsigned int from = 0;
		unsigned int dst = 0;
		if( !PyArg_ParseTuple(pyArgs, "II", &from, &dst) )
			Py_RETURN_NONE;

		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  insertQuadFromIndex, from, dst);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, removeQuadAtIndex)
	{
		unsigned int dst = 0;
		if( !PyArg_ParseTuple(pyArgs, "I", &dst) )
			Py_RETURN_NONE;

		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  removeQuadAtIndex, dst);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, removeQuadsAtIndex)
	{
		unsigned int i = 0;
		unsigned int ac = 0;
		if( !PyArg_ParseTuple(pyArgs, "II", &i, &ac) )
			Py_RETURN_NONE;

		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  removeQuadsAtIndex, i, ac);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, removeAllQuads)
	{
		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  removeAllQuads);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, resizeCapacity)
	{
		unsigned int cap = 0;
		if( !PyArg_ParseTuple(pyArgs, "I", &cap) )
			Py_RETURN_FALSE;

		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  resizeCapacity, bool, cap);		
		Py_RETURN_BOOL(v);
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, increaseTotalQuadsWith)
	{
		unsigned int ac = 0;
		if( !PyArg_ParseTuple(pyArgs, "I", &ac) )
			Py_RETURN_NONE;

		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  increaseTotalQuadsWith, ac);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, fillWithEmptyQuadsFromIndex)
	{
		unsigned int i = 0;
		unsigned int ac = 0;
		if( !PyArg_ParseTuple(pyArgs, "II", &i, &ac) )
			Py_RETURN_NONE;

		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  fillWithEmptyQuadsFromIndex, i, ac);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, drawQuads)
	{
		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  drawQuads);		
		Py_RETURN_NONE;
	}

    PYCLASS_METHOD_IMPL(CPyTextureAtlas, listenBackToForeground)
	{
		PyObject *py = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &py) )
			Py_RETURN_NONE;
		
		CPyCCObject *pyCC = (CPyCCObject *)py;

		CC_METHOD_WRAPPER_SETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  listenBackToForeground, pyCC->getCCObject());		
		Py_RETURN_NONE;
	}

	
    PYCLASS_GET_IMPL(CPyTextureAtlas, _pIndices)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  get_m_pIndices, GLushort *);
		CPyPointer *pt = CPyPointer::create(v);
		return (PyObject *)pt;
	}

    PYCLASS_SET_IMPL(CPyTextureAtlas, _pIndices)
	{
		if( !(PyObject_TypeCheck(pyValue, CPyClassBase::GetTypeObject<CPyPointer>())) )
			return 0;

		CPyPointer *pt = (CPyPointer *)pyValue;
		CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas, set_m_pIndices, (GLushort *)pt->getPtr());
		return 0;
	}

#if CC_TEXTURE_ATLAS_USE_VAO
    PYCLASS_GET_IMPL(CPyTextureAtlas, _uVAOname)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  get_m_uVAOname, GLuint);
		return Py_BuildValue("I", v);
	}

    PYCLASS_SET_IMPL(CPyTextureAtlas, _uVAOname)
	{
		GLuint st = (GLuint)PyLong_AsUnsignedLong(pyValue);
		CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas, set_m_uVAOname, st);
		return 0;
	}
#endif
    PYCLASS_GET_IMPL(CPyTextureAtlas, _pBuffersVBO_1)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  get_m_pBuffersVBO,  GLuint, 0);
		return Py_BuildValue("I", v);
	}

    PYCLASS_SET_IMPL(CPyTextureAtlas, _pBuffersVBO_1)
	{
		GLuint st = (GLuint)PyLong_AsUnsignedLong(pyValue);
		CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas, set_m_pBuffersVBO, 0, st);
		return 0;
	}

    PYCLASS_GET_IMPL(CPyTextureAtlas, _pBuffersVBO_2)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  get_m_pBuffersVBO,  GLuint, 1);
		return Py_BuildValue("I", v);
	}

    PYCLASS_SET_IMPL(CPyTextureAtlas, _pBuffersVBO_2)
	{
		GLuint st = (GLuint)PyLong_AsUnsignedLong(pyValue);
		CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas, set_m_pBuffersVBO, 1, st);
		return 0;
	}

    PYCLASS_GET_IMPL(CPyTextureAtlas, _bDirty)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  get_m_bDirty,  bool);
		Py_RETURN_BOOL(v);
	}

    PYCLASS_SET_IMPL(CPyTextureAtlas, _bDirty)
	{
		bool b = pyValue == Py_True;
		CC_METHOD_WRAPPER_SETER_RINT(__CCTextureAtlasWrapper *, CPyTextureAtlas, set_m_bDirty, b);
		return 0;
	}

    PYCLASS_GET_IMPL(CPyTextureAtlas, _TotalQuads)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  _TotalQuads,  unsigned int);
		return Py_BuildValue("I", v);
	}

    PYCLASS_GET_IMPL(CPyTextureAtlas, _Capacity)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  _Capacity,  unsigned int);
		return Py_BuildValue("I", v);
	}

    PYCLASS_GET_IMPL(CPyTextureAtlas, _Texture)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  _Texture,  cocos2d::CCTexture2D *);
		CPyTexture2D *tex = CPyCCObject::create<CPyTexture2D>(v);
		return (PyObject *)tex;
	}

    PYCLASS_GET_IMPL(CPyTextureAtlas, _Quads)
	{
		CC_METHOD_WRAPPER_GETER(__CCTextureAtlasWrapper *, CPyTextureAtlas,  _Quads,  cocos2d::ccV3F_C4B_T2F_Quad *);
		CPyV3F_C4B_T2F_Quad *vt = CPyV3F_C4B_T2F_Quad::create(*v);
		return (PyObject *)vt;
	}

NS_CCPY_END

#endif